//
//  ScrollManager.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/15.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "ScrollManager.h"
#import "RecScrollModel.h"
@interface ScrollManager()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scroll;
@property (nonatomic,strong)UIPageControl *page;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *imageArray;

@property (nonatomic,strong)NSTimer *timer;

@property (nonatomic,strong)UIImageView *leftImage;
@property (nonatomic,strong)UIImageView *rightImage;


@end

@implementation ScrollManager
- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}




- (UIPageControl *)page{
    if (!_page) {
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(20, self.frame.size.height-50, 100, 50)];
        _page.numberOfPages = self.imageArray.count;
    }
    return _page;
}
- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (UIImageView *)leftImage{
    if (!_leftImage) {
        _leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        ///[_leftImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
        if (self.imageArray.count) {
            _leftImage.image = [UIImage imageWithData:[[NSData alloc] initWithBase64Encoding:self.imageArray[0]]];
        }
    }
    return _leftImage;
}
- (UIImageView *)rightImage{
    if (!_rightImage) {
        _rightImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
       /// [_rightImage sd_setImageWithURL:[NSURL URLWithString:self.imageArray[1]]];
        if (self.imageArray.count) {
            _rightImage.image = [UIImage imageWithData:[[NSData alloc] initWithBase64Encoding:self.imageArray[1]]];
        }
        
        
        
    }
    return _rightImage;
}

- (UIScrollView *)scroll{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        _scroll.contentSize = CGSizeMake(self.frame.size.width*2, self.frame.size.height);
        _scroll.pagingEnabled = YES;
        _scroll.bounces = NO;
        _scroll.bouncesZoom = NO;
        _scroll.delegate = self;
    }
    return _scroll;
}



- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray {

    self = [super initWithFrame:frame];
    if (self) {
        self.index = 0;
        for (RecScrollModel *model in imageArray) {
            [self.imageArray addObject:model.photo];
        }
        if (self.imageArray.count) {
            [self buildScrollView];
        
        [self addSubview:self.scroll];
        [self addSubview:self.page];
        }
        
      
    }
    
    return self;
}
//添加轮播视图
- (void)buildScrollView{
    
    
    [self.scroll addSubview:self.leftImage];
    [self.scroll addSubview:self.rightImage];
    [self.timer fire];
    
}
//定时器响应事件
- (void)timerAction{
    [self.scroll setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
    
}
#pragma mark---------scrollDelegate----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    
    if (self.scroll.contentOffset.x == self.frame.size.width) {
        
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.page.currentPage = self.index;
        
        
        
        
        self.leftImage.image = self.rightImage.image;
        self.scroll.contentOffset = CGPointZero;
        if (self.index+1 == self.imageArray.count) {
            self.rightImage.image = [UIImage imageWithData:[[NSData alloc] initWithBase64Encoding:self.imageArray[0]]];
            
        }else{
            
            self.rightImage.image = [UIImage imageWithData:[[NSData alloc] initWithBase64Encoding:self.imageArray[self.index+1]]];
        }
        
        self.index++;
    }
    
    [self.managerDelegate pushNextViewWithIndex:self.index];
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
